package hevs.eServices.client.ui;

import hevs.eServices.client.E_servicesService;
import hevs.eServices.client.E_servicesServiceAsync;

import java.util.Date;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.CloseHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.datepicker.client.DatePickerEx;

public class CalendarView {
	
	private Date fromDate;
	private Date toDate;
	private TextBox to;
	private TextBox from;
	
	public CalendarView(Date fromDate, Date toDate, TextBox from, TextBox to, boolean toCalendar){
		
		System.out.println("Calendar View created");
			
		this.fromDate = fromDate;
		this.toDate = toDate;
		this.to = to;
		this.from = from;
		
		
		DatePickerEx dp = new DatePickerEx(this.fromDate, this.toDate);
		dp.setCurrentMonth(this.fromDate);
		PopupPanel popup = new PopupPanel(true);
		popup.setWidget(dp);
		if(toCalendar)
			popup.setPopupPosition(this.to.getAbsoluteLeft()+3, this.to.getAbsoluteTop()+23);
		else
			popup.setPopupPosition(this.from.getAbsoluteLeft()+3, this.from.getAbsoluteTop()+23);
			
		popup.setStyleName("calendar_view");
		popup.show();
		
	
	    // Set the value in the text box when the user selects a date
		if(toCalendar)
			dp.addValueChangeHandler(new DateChangeHandler(this.to)); 
		else
			dp.addValueChangeHandler(new DateChangeHandler(this.from)); 
			
	    popup.addCloseHandler(new CalendarCloseHandler(from, to));
	
	}
	
	private class CalendarCloseHandler implements CloseHandler<PopupPanel>{
		
		TextBox from;
		TextBox to;
		
		public CalendarCloseHandler(TextBox from, TextBox to){
			
			this.from = from;
			this.to = to;
			
		}

		@Override
		public void onClose(CloseEvent<PopupPanel> event) {
			// TODO Auto-generated method stub
			
			//Date d1 = DateTimeFormat.getFormat("dd.MM.yyyy").parse(from.getText());
			//Date d2 = DateTimeFormat.getFormat("dd.MM.yyyy").parse(to.getText());
			Date d1 = DateTimeFormat.getFormat("yyyy-MM-dd").parse(from.getText());
			Date d2 = DateTimeFormat.getFormat("yyyy-MM-dd").parse(to.getText());
			
			if(d1!= null && d2!= null){
				if(d1.after(d2)){
					System.out.println(from.getText() +" a "+to.getText());
					System.out.println("La date de fin ne peut pas etre apres la date de debut");
				}
			}
		}
		
	}
	
	private class DateChangeHandler implements ValueChangeHandler<Date>{
		
		private TextBox box;
		
		public DateChangeHandler(TextBox box){
			super();
			this.box = box;
		}

		@Override
		public void onValueChange(ValueChangeEvent<Date> event) {
			// TODO Auto-generated method stub
			Date date = (Date) event.getValue();
	        //String dateString = DateTimeFormat.getFormat("dd.MM.yyyy").format(date);
			String dateString = DateTimeFormat.getFormat("yyyy-MM-dd").format(date);
	        this.box.setText(dateString);
			
		}
		
	}

}
